package com.beagle.njddapp.core;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.LocationBean;
import com.beagle.njddapp.bean.UploadLocationBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.event.OilOrderEvent;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-22 15:44
 */
public class LocationUploadService extends Service {


    private ApiService apiService;
    private boolean isstart;
    private boolean isDestory;
    private  double lat=0,lnt=0;
    private Handler handler;

//    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        apiService = retrofit.create(ApiService.class);
        handler=new Handler(getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                uploadLocation();
            }
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (handler!=null){
            handler.sendEmptyMessageDelayed(1,1000);
        }else {
            handler=new Handler(getMainLooper()){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    super.handleMessage(msg);
                    uploadLocation();
                }
            };
            handler.sendEmptyMessageDelayed(1,1000);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d("onDestroy");
        handler.removeCallbacksAndMessages(null);
        isDestory=true;
    }
    public void uploadLocation(){
        if(TextUtils.isEmpty(UserService.getInstance().getToken())){
            handler.sendEmptyMessageDelayed(1,5000);
            return;
        }
        if(lat==0){
            lat=LocationManager.getInstance().getLatitude();
            lnt=LocationManager.getInstance().getLongitude();
        }
        double realLat,realLnt;
        realLat=LocationManager.getInstance().getLatitude();
        realLnt=LocationManager.getInstance().getLongitude();
        if(realLat==0){
            realLat=lat;
        }else {
            lat=realLat;
        }
        if(realLnt==0){
            realLnt=lnt;
        }else {
            lnt=realLnt;
        }
        if(realLat==0||realLnt==0){
            handler.sendEmptyMessageDelayed(1,5000);
            return;
        }
        UploadLocationBean locationBean = new UploadLocationBean();
        locationBean.setLatitude(realLat);
        locationBean.setLongitude(realLnt);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), new Gson().toJson(locationBean));
        apiService.updateDeviceLocation(body).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).safeSubscribe(new HttpResponseObserver<BaseHttpResponse>() {

            @Override
            public void success(BaseHttpResponse baseHttpResponse) {
                handler.sendEmptyMessageDelayed(1,5000);
            }

            @Override
            public void error(int code, String message) {
                handler.sendEmptyMessageDelayed(1,5000);
            }
        });
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
